define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/change/index' + location.search,
                    // add_url: 'purchase/change/add',
                    // edit_url: 'purchase/change/edit',
                    // del_url: 'purchase/change/del',
                    // multi_url: 'purchase/change/multi',
                    table: 'purchase_change',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'change_id'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'collectiontime', title: __('Collectiontime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'explain', title: __('Explain')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'status', title: __('Status')},
                        {field: 'tax', title: __('Tax'), operate:'BETWEEN'},
                        {field: 'purchaseenter.number', title: __('Purchaseenter.number')},
                        {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'storehouse.name', title: __('Storehouse.name')},
                        {field: 'purchaseorder.number', title: __('Purchaseorder.number')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('采购单审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var oid =$(this).parents('tr').find('.oid').html()
                                        // console.log(oid)
                                        // var url = Config.this_url+'/purchase/order/enter?ids='+oid
                                        layer.confirm('是否通过该采购单？', {
                                            btn: ['确定','驳回'] //按钮
                                        }, function(){
                                            var url = Config.this_url+'/purchase/order/examine?ids='+oid
                                            $.post(url,{status:1},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        }, function(){
                                            var url = Config.this_url+'/purchase/order/examine?ids='+oid
                                            $.post(url,{status:2},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        });

                                    }},

                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var change_id = $(this).parents('tr').find('.change_id').html()
                var url = Config.this_url+'/purchase/change/detail?ids='+change_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "退货详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});